/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ymtweet;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.User;
import util.Log;
import util.StringParser;
import ymsg.network.Session;
import ymsg.network.event.SessionEvent;
import org.netbeans.saas.twitter.TwitterWhatAreYouDoingService;
import org.netbeans.saas.RestResponse;

/**
 *
 * @author leswid
 */
public class TwitterThread extends Thread {

    private Main main;
    private SessionEvent se;

    public TwitterThread(Main main, SessionEvent se) {
        this.main = main;
        this.se = se;
    }

    @Override
    public void run() {
        filter();
    }

    //filter first token
    private void filter() {
        String token = new StringParser(se.getMessage(), " ").getListToken().get(0);

        if (token.equalsIgnoreCase("login")) {
            login();

        } else if (token.equalsIgnoreCase("logout")) {
            logout();

        } else {
            tweet();

        }
    }

    //service
    private void login() {
        //cek apa sudah login
        User user = (User) main.getTable().get(se.getFrom());
        Session session = main.getSession();

        if (user != null) {
            //sudah login
            //set message
            Log.print("sudah login...");
        } else {
            Log.print("belum login...");
            //belum login
            //persiapan username twitter
            String twitterUsername = new StringParser(se.getMessage(), " ").getListToken().get(1);
            String twitterPassword = new StringParser(se.getMessage(), " ").getListToken().get(2);

            //loginkan di hashtable
            user = new User(se.getFrom(), twitterUsername, twitterPassword);
            main.getTable().put(se.getFrom(), user);

        }
        sendMessage(se.getFrom(), "You are logged in");
    }

    private void logout() {
        //langsung di delete dari table
        main.getTable().remove(se.getFrom());

        //kirim message
        sendMessage(se.getFrom(), "You are logged out");
        Log.print("logout...");
    }

    private void tweet() {
        Log.print("twitting...");
        User user = (User) main.getTable().get(se.getFrom());
        if (user != null) {
            try {
                String status = se.getMessage();
                String inReplyToStatusId = null;
                String format = "xml";

                RestResponse result = TwitterWhatAreYouDoingService.updateStatus(user, status, inReplyToStatusId, format);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private void sendMessage(String username, String message) {
        Session session = main.getSession();
        try {
            session.sendMessage(se.getFrom(), message);
        } catch (IllegalStateException ex) {
            Logger.getLogger(TwitterThread.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TwitterThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
